import { combineReducers } from 'redux';
import { persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import { routerReducer } from '../history';
// WHITELIST
//持久化存储
export const persistConfig = {
  key: '__info__',
  storage: storage,
  whitelist: ['loginInfo', 'lastFetchedTime', 'tenantInfo', 'projectInfo']
};

const context = import.meta.glob<{ default: string }>('./*.ts', { eager: true })
const reducers: { [key: string]: any } = {}
const keys = Object.keys(context)
keys.map((filename) => {
  if (filename !== './index.ts' && filename !== './types.d.ts') {
    const name = filename.replace(/\.\/|\.ts/g, '');
    reducers[name] = context[filename].default
  }
})
const createRootReducer = combineReducers({
  ...reducers,
  common: persistReducer<initStateType>(persistConfig, reducers.common),
  router: routerReducer,
})

export default createRootReducer